﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Sale
{
    public partial class HeTong_GongYePin_MaiMai_SheZhi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             int BillID = 0;
             if (!this.IsPostBack)
             {
                 if (Request.QueryString["ID"] != null)
                 {
                     int.TryParse(Request.QueryString["ID"], out BillID);
                 }               
                 if (BillID > 0)
                 {
                     SqlConnection Conn = new SqlConnection();
                     Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                     Conn.Open();
                     SqlCommand Cmd = new SqlCommand();
                     Cmd.Connection = Conn;
                     Cmd.CommandText = @"Select 
                     [原料采购合同].[原料ID],
                     [原料采购合同].[合同编号], 
                     [原料采购合同].[供货单位ID] ,
                     [原料].[品名],
                     [原料].规格,
                     [原料].单位,
                     [原料采购合同].签订日期,
                      数量,
                     [原料采购合同].单价,
                     金额,
                     长度,
                     交货时间,
                     交货数量,
                     [原料采购合同].[数量] as [采购量], 
                     [原料采购合同].[签订日期] 
                     from [原料采购合同],[原料供货单位],[原料]
                     where [原料供货单位].[ID]= [原料采购合同].[供货单位ID]
                     and [原料].[ID]= [原料采购合同].[原料ID] and [原料采购合同].[合同编号]=(select 合同编号 from 原料采购合同 where 原料采购合同.ID=" + BillID + ")";
                     Cmd.CommandType = CommandType.Text;
                     SqlDataReader Reader = Cmd.ExecuteReader();
                     for (int i = 1; i <= 1; i++)
                     {
                        if (!Reader.Read())
                         {
                              break;
                         }
                             this.ViewState["BillID"] = BillID;
                             this.GongHuoFang.Text = Reader.GetInt32(2).ToString(); 
                             this.HeTongHao.Text = Reader.GetString(1);
                            
                             this.RiQi.Text = Reader.GetDateTime(6).ToString("D");
                            
                             this.ViewState["id" + i] = Reader.GetInt32(0).ToString();
                             if (Reader.IsDBNull(3))
                             {
                                 ((TextBox)this.FindControl("R" + i + "_C1")).Text = "";
                             }
                             else
                             {
                                 ((TextBox)this.FindControl("R" + i + "_C1")).Text = Reader.GetString(3);
                             }
                             ((TextBox)this.FindControl("R" + i + "_C3")).ReadOnly = false;
                             ((TextBox)this.FindControl("R" + i + "_C3")).CssClass = "intxt1";
                             if (Reader.IsDBNull(4))
                             {
                                 ((TextBox)this.FindControl("R" + i + "_C2")).Text = "";
                             }
                             else
                             {
                                 ((TextBox)this.FindControl("R" + i + "_C2")).Text = Reader.GetString(4);
                             }
                             ((TextBox)this.FindControl("R" + i + "_C4")).Text = Reader.GetString(5);
                             if (Reader.IsDBNull(7))
                             {
                                 ((TextBox)this.FindControl("R" + i + "_C3")).Text = "";
                             }
                             else
                             {
                                 ((TextBox)this.FindControl("R" + i + "_C3")).Text = Reader.GetSqlSingle(7).ToString();
                             }
                             if (Reader.IsDBNull(8))
                             {
                                 ((TextBox)this.FindControl("R" + i + "_C5")).Text = "";
                             }
                             else
                             {
                                 ((TextBox)this.FindControl("R" + i + "_C5")).Text = Reader.GetSqlMoney(8).ToString();
                             }
                             ((TextBox)this.FindControl("R" + i + "_C5")).ReadOnly = false;
                             ((TextBox)this.FindControl("R" + i + "_C5")).CssClass = "intxt1";
                             //if (Reader.IsDBNull(9))
                             //{
                             //    ((TextBox)this.FindControl("R" + i + "_C6")).Text = "";
                             //}else
                             //{
                             //    ((TextBox)this.FindControl("R" + i + "_C6")).Text = Reader.GetSqlMoney(9).ToString();
                             //}
                             if (Reader.IsDBNull(10))
                             {
                                 ((TextBox)this.FindControl("R" + i + "_C7")).Text = "";
                             }
                             else
                             {
                                 ((TextBox)this.FindControl("R" + i + "_C7")).Text = Reader.GetDouble(10).ToString();
                             }
                             ((TextBox)this.FindControl("R" + i + "_C7")).ReadOnly = false;
                             ((TextBox)this.FindControl("R" + i + "_C7")).CssClass = "intxt1";
                             string date = Reader.GetString(11).ToString();
                             ((TextBox)this.FindControl("R" + i + "_C9")).Text = date;
                             ((TextBox)this.FindControl("R" + i + "_C9")).CssClass = "intxt1";
                             ((TextBox)this.FindControl("R" + i + "_C9")).Enabled = true;
                             ((TextBox)this.FindControl("R" + i + "_C9")).ReadOnly = false;
                            // ((DropDownList)this.FindControl("DropDownList" + i)).Visible = true;
                             //((DropDownList)this.FindControl("DropDownList" + i)).SelectedValue = Reader.GetString(11).ToString();
                             if (Reader.IsDBNull(10))
                             {
                                 ((TextBox)this.FindControl("R" + i + "_C8")).Text = "";
                             }
                             else
                             {
                                 ((TextBox)this.FindControl("R" + i + "_C8")).Text = Reader.GetDouble(12).ToString();
                             }
                             ((TextBox)this.FindControl("R" + i + "_C8")).ReadOnly = false;
                             ((TextBox)this.FindControl("R" + i + "_C8")).CssClass = "intxt1";                          
                             ((Button)this.FindControl("Button_Add" + i)).Text = "删 除";
                         }
                         Reader.Close();
                         Conn.Close();
                         Reader.Dispose();
                         Cmd.Dispose();
                         Conn.Dispose();
                     
                 }
             }
        }
        protected void Button_Add_Command(object sender, CommandEventArgs e)
        {
            int row;
            int.TryParse(e.CommandArgument.ToString(), out row);
            this.ViewState["row"] = row;

            if (this.ViewState["id" + row] == null)
            {
                this.Panel_Grid.Visible = true;
                this.Message.Text = "·准备添加";
            }
            else
            {
                this.ViewState["id" + row] = null;
                ((TextBox)this.FindControl("R" + row + "_C1")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C2")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C3")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C3")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C3")).CssClass = "blank_center";

                ((TextBox)this.FindControl("R" + row + "_C4")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C4")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C4")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C5")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C5")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C5")).Text = "";
                //((TextBox)this.FindControl("R" + row + "_C6")).ReadOnly = true;
                //((TextBox)this.FindControl("R" + row + "_C6")).CssClass = "blank_center";
                //((TextBox)this.FindControl("R" + row + "_C6")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C7")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C7")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C7")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C8")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C8")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C8")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C9")).Enabled = true;
                ((TextBox)this.FindControl("R" + row + "_C9")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C9")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C9")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C9")).Enabled = false;
              //  ((DropDownList)this.FindControl("DropDownList" + row)).Visible = false;               
                ((Button)this.FindControl("Button_Add" + row)).Text = "添 加";
                this.Message.Text = "·删除成功";
            }
        }

        protected void ImageButton_Cancel_Click(object sender, ImageClickEventArgs e)
        {
            this.Panel_Grid.Visible = false;
            this.Message.Text = "·已取消添加";
        }
        protected void Button1_Command(object sender, CommandEventArgs e)
        {

            int row = (int)this.ViewState["row"];
            string[] Args = e.CommandArgument.ToString().Split("\t".ToCharArray());

            this.ViewState["id" + row] = Args[0];
            ((TextBox)this.FindControl("R" + row + "_C1")).Text = Args[1];
            ((TextBox)this.FindControl("R" + row + "_C2")).Text = Args[3];
            ((TextBox)this.FindControl("R" + row + "_C3")).Text = "";
            ((TextBox)this.FindControl("R" + row + "_C3")).ReadOnly = false;
            ((TextBox)this.FindControl("R" + row + "_C3")).CssClass = "intxt1";
            ((TextBox)this.FindControl("R" + row + "_C4")).Text = Args[2]; 
            ((TextBox)this.FindControl("R" + row + "_C5")).Text = "";
            ((TextBox)this.FindControl("R" + row + "_C5")).ReadOnly = false;
            ((TextBox)this.FindControl("R" + row + "_C5")).CssClass = "intxt1";
           // ((TextBox)this.FindControl("R" + row + "_C6")).Text = "";
           // ((TextBox)this.FindControl("R" + row + "_C6")).ReadOnly = false;
           // ((TextBox)this.FindControl("R" + row + "_C6")).CssClass = "intxt1";
            ((TextBox)this.FindControl("R" + row + "_C7")).Text = "";
            ((TextBox)this.FindControl("R" + row + "_C7")).ReadOnly = false;
            ((TextBox)this.FindControl("R" + row + "_C7")).CssClass = "intxt1";
            ((TextBox)this.FindControl("R" + row + "_C8")).Text = "";
            ((TextBox)this.FindControl("R" + row + "_C8")).ReadOnly = false;
            ((TextBox)this.FindControl("R" + row + "_C8")).CssClass = "intxt1";
            ((TextBox)this.FindControl("R" + row + "_C9")).Enabled = true;
            ((TextBox)this.FindControl("R" + row + "_C9")).Text = "";
            ((TextBox)this.FindControl("R" + row + "_C9")).ReadOnly = false;
            ((TextBox)this.FindControl("R" + row + "_C9")).CssClass = "intxt1";
           // ((DropDownList)this.FindControl("DropDownList" + row)).Visible = true;           
            ((Button)this.FindControl("Button_Add" + row)).Text = "删 除";
            this.Panel_Grid.Visible = false;
            this.Message.Text = "·添加成功";
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }

        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }
        }

        protected void Button_Finish_Click(object sender, EventArgs e)
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlTransaction Tran = Conn.BeginTransaction();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.Transaction = Tran;
            try
            {             
                    Cmd.CommandText = "CaiGou_HeTong_MaiMai_In";
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.Clear();
                    if (this.ViewState["BillID"] == null)
                    {
                        this.ViewState["BillID"] = -1;
                    }
                    Cmd.Parameters.Add(new SqlParameter("@供货商ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                    Cmd.Parameters.Add(new SqlParameter("@合同编号", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, null));
                    Cmd.Parameters.Add(new SqlParameter("@签订日期", SqlDbType.Date, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, null));
                    Cmd.Parameters.Add(new SqlParameter("@原料ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                    Cmd.Parameters.Add(new SqlParameter("@数量", SqlDbType.Real, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, null));
                    Cmd.Parameters.Add(new SqlParameter("@单价", SqlDbType.Money, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, null));
                    Cmd.Parameters.Add(new SqlParameter("@金额", SqlDbType.Money, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                    Cmd.Parameters.Add(new SqlParameter("@长度", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                    Cmd.Parameters.Add(new SqlParameter("@交货时间", SqlDbType.NVarChar, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, null));
                    Cmd.Parameters.Add(new SqlParameter("@交货数量", SqlDbType.Float, 150, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, null));
                    Cmd.Parameters.Add(new SqlParameter("@BillID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.ViewState["BillID"]));
                    Cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));
                 
                    for (int i = 1; i <= 1; i++)
                    {
                        if ((string)ViewState["id" + i] != null)
                        {
                            Cmd.Parameters["@供货商ID"].Value = int.Parse(this.GongHuoFang.SelectedValue);
                            Cmd.Parameters["@合同编号"].Value = this.HeTongHao.Text;
                            Cmd.Parameters["@签订日期"].Value = DateTime.Parse(this.RiQi.Text);
                            Cmd.Parameters["@原料ID"].Value = int.Parse((string)ViewState["id" + i]);
                            string 数量 = ((TextBox)this.FindControl("R" + i + "_C3")).Text;
                            if (((TextBox)this.FindControl("R" + i + "_C3")).Text== "")
                            {
                                this.Message.Text = ".请输入数量";
                                return;
                            }
                            else
                            {
                                if (int.Parse(数量) < 0)
                                {
                                    this.Message.Text = ".数量不能小于0";
                                    return;
                                }
                            }
                            Cmd.Parameters["@数量"].Value = int.Parse(数量);
                            Cmd.Parameters["@单价"].Value = Decimal.Parse(((TextBox)this.FindControl("R" + i + "_C5")).Text);
                            Cmd.Parameters["@金额"].Value = int.Parse(((TextBox)this.FindControl("R" + i + "_C3")).Text) * (Decimal.Parse(((TextBox)this.FindControl("R" + i + "_C5")).Text));
                            string 长度 = ((TextBox)this.FindControl("R" + i + "_C7")).Text;
                            if (((TextBox)this.FindControl("R" + i + "_C7")).Text == "")
                            {
                                this.Message.Text = ".长度不能为空";
                                return;
                            }
                            else
                            {
                                if (float.Parse(长度) < 0)
                                {
                                    this.Message.Text = ".长度不能小于0";
                                    return;
                                }
                            }
                            Cmd.Parameters["@长度"].Value = float.Parse(长度);
                            Cmd.Parameters["@交货时间"].Value = ((TextBox)this.FindControl("R" + i + "_C9")).Text;
                            string 交货数量 = ((TextBox)this.FindControl("R" + i + "_C8")).Text;
                            if (((TextBox)this.FindControl("R" + i + "_C8")).Text == "")
                            {
                                this.Message.Text = ".交货数量不能为空";
                                return;
                            }
                            else
                            {
                                if (int.Parse(交货数量) < 0)
                                {
                                    this.Message.Text = ".交货数量不能小于0";
                                    return;
                                }
                            }
                            Cmd.Parameters["@交货数量"].Value = ((TextBox)this.FindControl("R" + i + "_C8")).Text;
                            this.ViewState["BillID"] = (int)Cmd.Parameters["@供货商ID"].Value;
                            int BillID = (int)this.ViewState["BillID"];
                            Cmd.ExecuteNonQuery();
                        }
                    }
                    this.Message.Text += "·保存成功";          
                   Tran.Commit();
            }
            catch (Exception err)
            {
                Tran.Rollback();
               this.Message.Text = err.Message;
            }
             Cmd.Dispose();
            Tran.Dispose();
            Conn.Close();
            Conn.Dispose();            
            //Response.Redirect("HeTong_YuanLiao_Search.aspx");
        }

        protected void ToWord_Click(object sender, EventArgs e)
        {
            
        }

      
    }
}
